<?php
// View for admin - scores
require_once WACT_ROOT.'controller/controller.inc.php';
require_once WACT_ROOT .'controller/form.inc.php';
require_once WACT_ROOT.'view/view.inc.php';
require_once WACT_ROOT .'view/formview.inc.php';

require_once APP_ROOT . 'view/common/layout.formview.php';
require_once APP_ROOT . 'view/common/layout.view.php';

require_once 'model/common/user.php';
require_once 'model/common/statdivisions.php';
require_once 'model/common/statteams.php';
require_once 'model/common/statscores.php';
require_once 'model/common/categories.php';
require_once 'model/common/date.php';


class ScoresListView extends LayoutFormView {

    function ScoresListView($TemplateFile) {
    	//parent :: $IsAdminView = true;
        parent :: LayoutFormView($TemplateFile);
    }



    function prepare(&$controller, &$request, &$responseModel) {
    	parent::prepare($controller, $request, $responseModel);
        $pager =& $this->Template->getChild('pagenav');
        $pager2 =& $this->Template->getChild('pagenav2');
        $list =& Score::getScoresList($pager,$request,$responseModel);
        $list->paginate($pager2);


        $list_updated=array();


        $list->reset();
        while ($list->next()) {

        	$dataspace = $list->export();

            $scoreID = $list->get('scoreID');

            $enable = $list->get('enabled');
            if ($enable) $dataspace['status_show'] =  'enabled';
            else $dataspace['status_show'] =  'disabled';
            if ($enable) $dataspace['enabled_show'] =  1;

            //print_r($dataspace);
            $list_updated[$scoreID] = $dataspace;
        }


        $list_source_updated =& new ArrayDataSet($list_updated);
        $this->Template->setChildDataSource('List', $list_source_updated);

        //populate sort combo
        $sortby =& $this->Template->getChild('sort');
        $sort_list = array('S.scoreID'=>'ID', 'D1.divisionName'=>'Division', 'T1.teamName, T2.teamName'=>'Team Name','S.date_added desc'=>'Date Posted','S.gameDate desc'=>'Game Date');
        $sortby->setChoices($sort_list);

        //populate divisions
        $divisions =& Division::getDivisionsForList();
        //print_r($divisions);
        $division =& $this->Template->getChild('division_id');
        $division->setChoices($divisions);

         //populate teams
        $teams =& StatTeam::getTeamsForList();
        //print_r($teams);
        $team =& $this->Template->getChild('teamID');
        $team->setChoices($teams);

        // populate season Years
        $years =& StatTeam::getSeasonYears();
        $season =& $this->Template->getChild('seasonYear');
        $season->setChoices($years);

        //populate months
        $start_month =& $this->Template->getChild('start_month');
        $end_month =& $this->Template->getChild('end_month');
        $months_list =& SportDate::getMonthsList();
        $start_month->setChoices($months_list);
        $end_month->setChoices($months_list);

        //prepopulate start and end date
        $cur_month = (integer)date("m");
        $cur_year = (integer)date("Y");
        $cur_day = (integer)date("d");

        if ( $cur_month==1 ) {
        	$prev_month = 12;
        	$prev_year = $cur_year-1;
        }  else {
           $prev_month = $cur_month - 1;
           $prev_year = $cur_year;
        }

        if ( $cur_month==12 ) {
        	$next_month = 1;
        	$next_year = $cur_year+1;
        }  else {
           $next_month = $cur_month + 1;
           $next_year = $cur_year;
        }

        $start_day =& $this->Template->getChild('start_day');
        $day = $start_day->getValue();
        if (!$day) $start_day->setValue(1);

        $end_day =& $this->Template->getChild('end_day');
        $day = $end_day->getValue();
        if (!$day) $end_day->setValue($cur_day);

        //print $start_month->getValue();
        if ($start_month->getValue()<=0) $start_month->setSelection($prev_month);
        if ($end_month->getValue()<=0) {
        	//print $cur_month;
        	$end_month->setSelection($cur_month);
        }

        $start_year =& $this->Template->getChild('start_year');
        $year = $start_year->getValue();
        if (!$year) $start_year->setValue($prev_year);

        $end_year =& $this->Template->getChild('end_year');
        $year = $end_year->getValue();
        if (!$year) $end_year->setValue($cur_year);



    }

}






?>